package com.lingnan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingnan.mapper.ActiveMapper;
import com.lingnan.mapper.UserMapper;
import com.lingnan.pojo.Active;
import com.lingnan.pojo.Receive;
import com.lingnan.mapper.ReceiveMapper;
import com.lingnan.pojo.User;
import com.lingnan.service.ReceiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;


/**
 * <p>
 * 收到表 服务实现类
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@Service
public class ReceiveServiceImpl extends ServiceImpl<ReceiveMapper, Receive> implements ReceiveService {

    @Autowired
    ReceiveMapper receiveMapper;


    @Autowired
    UserMapper userMapper;

    @Autowired
    ActiveMapper activeMapper;

    @Override
    public List<User> findReplyUserByInfoId(Integer infoId) {
        ConcurrentHashMap<String, Object> hashMap = new ConcurrentHashMap<>();
        hashMap.put("info_id", infoId);
        List<Receive> receives = receiveMapper.selectByMap(hashMap);
        List list = new ArrayList();
        for (Receive receive : receives) {
            list.add(receive.getNo());
        }
        if (list.size() > 0){
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("no", list);
            List<User> userList = userMapper.selectList(queryWrapper);
            return userList;
        }
        return null;
    }

    @Override
    public List<User> findNoReplyUserByInfoId(Integer infoId) {
        Active active = activeMapper.selectById(infoId);
        String grade = active.getGrade();
        String majorId = active.getMajorId().toString();
        String like = grade + "__" + majorId + "%";
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("no", like);
        //获得该通知的所有用户
        List<User> userList = userMapper.selectList(queryWrapper);
        //获得该通知已经点击收到的用户
        List<User> replyUserByInfoId = findReplyUserByInfoId(infoId);
        if (replyUserByInfoId != null){
            userList.removeAll(replyUserByInfoId);
        }
        return userList;
    }

    @Override
    public Receive findByNoAndInfoId(Receive receive) {
        QueryWrapper<Receive> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("no", receive.getNo()).eq("info_id", receive.getInfoId());
        return receiveMapper.selectOne(queryWrapper);
    }
}
